<?php

class JobService extends BaseService {
	
    public function getJobById($jobId) {
        $job = Job::model()->findByPk($jobId);

        return $this->toArray($job);
    }    

	public function getJobs($conditions, $with = null) {
        if ($conditions instanceof CDbCriteria) {
           $criteria = $conditions;
        } 
        else {
            $criteria = $this->toCriteria($conditions);
        }
		try{
			if($with != null) {
				$jobs = Job::model()->with($with)->findAll($criteria);
				return $this->toArray($jobs, $with);
			} 
			else {
				$jobs = Job::model()->findAll($criteria);
				return $this->toArray($jobs); 
			}
		}
		catch(Exception $e) {
			Yii::log('get jobs failed:'. $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getJobCount($conditions, $with = null) {
		if ($conditions instanceof CDbCriteria) {
           $criteria = $conditions;
        } 
        else {
            $criteria = $this->toCriteria($conditions);
        }
		
		if($with != null) {
			return Job::model()->with($with)->count($criteria);
		} 
		else {
			return Job::model()->count($criteria);
		}
	}
	
	public function getAllFreqencies() {
		try {
			$jobs = Job::model()->findAll(
				array(
					'select' 	=>	'job_frequency',
					'distinct'	=>	true,
					'condition'	=>	'job_frequency > 0',
			));
			
			$jobFreqencies = array();
			foreach($jobs as $job) {
				$jobFreqencies[] = $job->job_frequency;
			}
			return $jobFreqencies;
		}
		catch(Exception $e) {
			Yii::log('get all frequencies error:'. $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getScheduleJobs ($freqencyList) {
		if (count($freqencyList) > 0) {
			$filter = array(
				array('job_frequency', 'in', $freqencyList),
                array('state', '!=', JobState::Removed),
			);
			$conditions = array(
                'condition'		=>	$filter,
			);
			$criteria = $this->toCriteria($conditions);
		} 
		else {
			$criteria = null;
		}
		
        $allJobs = $this->getJobs($criteria);
        return $allJobs;
	}

    public function getOneTimeJobs() {
        $conditions = array(
            'condition' =>  array(
                array('state', '=', JobState::Submit),
                array('job_frequency', '=', 0),
            )
        );
        return $this->getJobs($conditions);
    }

    public function editJobAttributes($jobId, $attributes) {
        return Job::model()->updateByPk($jobId, $attributes);
    }
	
	public function addJobDepend($jobDepend) {
		try {
			if(JobDepend::model()->countByAttributes(array(
				'job_id'		=>	$jobDepend['job_id'],
				'res_id'		=>	$jobDepend['res_id'],
				'type'			=>	$jobDepend['type'],
				'depend'		=>	$jobDepend['depend'],
			)) == 0) {
				$newJobDepend = new JobDepend;
				$newJobDepend->setAttributes($jobDepend, false);
				return $newJobDepend->save(); 
			}
			else {
				return true;
			}
		}
		catch (Exception $e) {
			Yii::log("add job depend error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function addJobOutput($jobOutput) {
		try {
			if(JobOutput::model()->countByAttributes(array(
				'job_id'		=>	$jobOutput['job_id'],
				'res_id'		=>	$jobOutput['res_id'],
			)) == 0) {
				$newJobOutput = new JobOutput;
				$newJobOutput->setAttributes($jobOutput, false);
				return $newJobOutput->save(); 
			}
			else {
				return true;
			}
		}
		catch (Exception $e) {
			Yii::log("add job output error:" . $e->getMessage(), CLogger::LEVEL_ERROR);
			return false;
		}
	}
	
	public function getJobDepends($jobId, $onlyId = false) {
		$jobDepends = JobDepend::model()->findAllByAttributes(array('job_id' => $jobId));
		
		if($onlyId) {
			$dependIds = array();
			foreach ($jobDepends as $jobDepend) {
				$dependIds[] = $jobDepend->res_id;	
			}
			return $dependIds;
		}
		else {
			return $this->toArray($jobDepends);
		}
	}	
}
